package com.zyc.studentstudy.utils

import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.text.format.Formatter
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

/**
 * 获取APP相关信息
 * Created by zhangyuncai on 2021/9/6.
 */
object AppUtils {

    @JvmStatic
    fun getContext(): Context {
        return ContextHolder.getApplicationContext()
    }

    @JvmStatic
    fun getPackageName(): String {
        return getContext().packageName
    }

    @JvmStatic
    fun getVersionName(): String {
        try {
            val packageInfo =
                getContext().packageManager.getPackageInfo(getContext().packageName, 0)
            return packageInfo.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return ""
    }

    @JvmStatic
    fun getVersionCode(): Long {
        try {
            val packageInfo =
                getContext().packageManager.getPackageInfo(getContext().packageName, 0)
            return packageInfo.versionCode.toLong()

        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return 1
    }

    @JvmStatic
    fun unstallApp(context: Context, pageName: String) {
        val intent = Intent()
        intent.action = Intent.ACTION_DELETE
        intent.setData(Uri.parse("package:${pageName}"))
        context.startActivity(intent)
    }

    /**
     * 获取当前时间值
     *
     * @return 当前时间
     */
    @JvmStatic
    fun getCurrentTime(): String? {
        val date = Date(System.currentTimeMillis())
        val sdf = SimpleDateFormat("yyyyMMdd HH:mm", Locale.getDefault())
        return sdf.format(date)
    }

    /**
     * 获取自定义格式的日期 "yyyyMMdd HH:mm"
     * @param formatType 自定义格式
     * @return 日期
     */
    @JvmStatic
    fun getCurrentTime(formatType: String?): String? {
        val date = Date(System.currentTimeMillis())
        val sdf = SimpleDateFormat(formatType, Locale.getDefault())
        return sdf.format(date)
    }

    /**
     * @return 返回内存信息
     */
    fun getAppMemoryUsage(context: Context): MemoryUsageBean {
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val pid = android.os.Process.myPid()
        val memoryInfoArray = activityManager.getProcessMemoryInfo(intArrayOf(pid))
        val totalPss = memoryInfoArray[0].totalPss.toLong() * 1024L
        val totalPssText = Formatter.formatFileSize(context, totalPss)

        val totalMem = Runtime.getRuntime().maxMemory()
        val totalMemText = Formatter.formatFileSize(context, totalMem)

        val percent = totalPss * 100L / totalMem
        val percentText = "${percent}%"

        return MemoryUsageBean(totalPss, totalMem, percent, totalPssText, totalMemText, percentText)

    }
}

data class MemoryUsageBean(
    val totalPss: Long,// 已用内存, // 单位：字节
    val totalMem: Long,// 总内存, // 单位：字节
    val percent: Long,//百分比
    val totalPssText: String,// 已用内存
    val totalMemText: String,// 总内存
    val percentText: String//百分比
)